package practica12;

import java.io.*;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;


public class Procesador {
	public static void main(String args[]){
		try {
			CharStream cs = new ANTLRFileStream(args[0]);
		    ExpresionesLexer analex = new ExpresionesLexer(cs);
		    CommonTokenStream tokens = new CommonTokenStream(analex);
		    ExpresionesParser anasint = new ExpresionesParser(tokens);
		    
		    anasint.setTreeAdaptor(new MyTreeAdaptor());
		   
		    ExpresionesParser.instrucciones_return exp_return = anasint.instrucciones();
		   
		    CommonTree t = (CommonTree) exp_return.getTree();
		    MyASTFrame miArbol = new MyASTFrame("Arithmetic Abstract Syntax Tree: " + args[0], t);
		    miArbol.setVisible(true);
		    
		    CommonTreeNodeStream nodes = new CommonTreeNodeStream((MyTree) exp_return.getTree());
            nodes.setTokenStream (tokens);

            Evaluador eval = new Evaluador(nodes);
            eval.setTreeAdaptor(new MyTreeAdaptor(args[0]));
           
            Evaluador.instrucciones_return re = eval.instrucciones();
   
            MyTree treeEval = (MyTree)re.getTree();
            System.out.println("Arbol evaluado"+ treeEval.toStringTree());
            MyASTFrame af0 = new MyASTFrame("Evaluated Tree", treeEval);

		}
		catch(IOException ioe) {
			System.err.println(ioe.getMessage());
		}
		catch(RecognitionException ae){
		   System.out.println(ae.getMessage());
	    }

	}
}
